class BMW:pass
class BYD:pass
class TESLA:pass
class BENZ:pass

'''
用类函数
'''
class Factory:
    @classmethod
    def createCar(cls, brand):
        if brand == '奔驰':
            return BENZ()
        elif brand == '宝马':
            return BMW()
        elif brand == '特斯拉':
            return TESLA()
        elif brand == '比亚迪':
            return BYD()
        else:
            raise Exception("品牌不存在，不能创建")


car1 = Factory.createCar("宝马")
car2 = Factory.createCar("比亚迪")
#print(car1, car2)
#car3 = Factory.createCar("比亚")

'''
单例+工厂
'''


# 工厂的实例只会建一次
class Factory2:
    __obj = None
    __init_flag = True

    # 自定义的new覆盖原本new, 又通过object详细路径调用原本的new
    def __new__(cls, *args, **kwargs):
        if cls.__obj == None:
            cls.__obj = object.__new__(cls)
        return cls.__obj

    def __init__(self): pass
    def __pri(self): pass

    def createCar(cls, brand):
        if brand == '奔驰':
            return BENZ()
        elif brand == '宝马':
            return BMW()
        elif brand == '特斯拉':
            return TESLA()
        elif brand == '比亚迪':
            return BYD()
        else:
            raise Exception("品牌不存在，不能创建")


f1 = Factory2()
c1 = f1.createCar("宝马")

f2 = Factory2()
c2 = f1.createCar("宝马")
print(dir(f2))
print(id(f1),id(f2))
